home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q1082.dms / q1082.adf / src.lzh / Fig / undo.c < prev    next >
C/C++ Source or Header  |  1991-07-18  |  13KB  |  468 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985, 1988 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : August 1985.
  7.  *    2nd revision : March 1988.
  8.  *
  9.  *    %W%    %G%
  10. */
  11. #include "fig.h"
  12. #include "resources.h"
  13. #include "func.h"
  14. #include "object.h"
  15. #include "paintop.h"
  16.  
  17. extern int        pointmarker_shown, compoundbox_shown;
  18.  
  19. extern int        foreground_color, background_color;
  20. extern int        last_action, last_object;
  21. extern int        last_axis, last_rotateangle;
  22. extern int        movedpoint_num;
  23. extern int        fix_x, fix_y;
  24. extern F_pos        last_position, new_position;
  25.  
  26. extern F_compound    objects;
  27. extern F_compound    saved_objects;
  28. extern F_compound    object_tails;
  29.  
  30. extern F_point        *left_point; 
  31. extern F_point        *moved_point;
  32. extern F_point        *deleted_point;
  33. extern F_point        *added_point;
  34.  
  35. undo()
  36. {
  37.     switch(last_action) {
  38.         case F_CREATE :
  39.         undo_create();
  40.         break;
  41.         case F_REMOVE :
  42.         undo_remove();
  43.         break;
  44.         case F_MOVE :
  45.         undo_move();
  46.         break;
  47.         case F_MOVE_POINT :
  48.         undo_movepoint();
  49.         break;
  50.         case F_FLIP :
  51.         undo_flip();
  52.         break;
  53.         case F_ROTATE :
  54.         undo_rotate();
  55.         break;
  56.         case F_GLUE :
  57.         undo_glue();
  58.         break;
  59.         case F_BREAK :
  60.         undo_break();
  61.         break;
  62.         case F_SCALE :
  63.         undo_scale();
  64.         break;
  65.         case F_ADD_POINT :
  66.         undo_addpoint();
  67.         break;
  68.         case F_DELETE_POINT :
  69.         undo_deletepoint();
  70.         break;
  71.         default :
  72.         return;
  73.         }    
  74.     }
  75.  
  76. undo_addpoint()
  77. {
  78.     if (last_object == O_POLYLINE)
  79.         linepoint_deleting(saved_objects.lines);
  80.     else
  81.         splinepoint_deleting(saved_objects.splines);
  82.     last_action = F_DELETE_POINT;
  83.     deleted_point = added_point;
  84.     }
  85.  
  86. undo_deletepoint()
  87. {
  88.     if (last_object == O_POLYLINE) 
  89.         linepoint_adding(saved_objects.lines, deleted_point);
  90.     else
  91.         splinepoint_adding(saved_objects.splines, deleted_point);
  92.     last_action = F_ADD_POINT;
  93.     added_point = deleted_point;
  94.     }
  95.  
  96. undo_break()
  97. {
  98.     if (compoundbox_shown)
  99.         draw_compoundbox(saved_objects.compounds, INV_PAINT);
  100.     cut_objects(&objects, &object_tails);
  101.     insert_compound(&objects.compounds, saved_objects.compounds);
  102.     last_action = F_GLUE;
  103.     }
  104.  
  105. undo_glue()
  106. {
  107.     if (compoundbox_shown)
  108.         draw_compoundbox(saved_objects.compounds, INV_PAINT);
  109.     delete_compound(&objects.compounds, saved_objects.compounds);
  110.     append_objects(&objects, saved_objects.compounds, &object_tails);
  111.     last_action = F_BREAK;
  112.     }
  113.  
  114. /* 
  115. When a single object is created, it is inserted as the first object in
  116. the appropriate list in objects.  It is also placed in the appropriate
  117. list in saved_objects.
  118.  
  119. However when a number of objects are created (usually by reading them
  120. in from a file or undoing a remove-all action), they are appended to
  121. the lists in objects and also saved in saved_objects.  The pointers
  122. in object_tails will be set to point to the last members of the lists
  123. in objects prior to the appending.
  124.  
  125. Note: The read operation will set the pointers in object_tails
  126. while the remove-all operation will zero pointers in objects.
  127. */
  128.  
  129. undo_create()
  130. {
  131.     switch(last_object) {
  132.         case O_POLYLINE :
  133.         objects.lines = saved_objects.lines->next;
  134.         saved_objects.lines->next = NULL;
  135.         erase_lines(saved_objects.lines);
  136.         break;
  137.         case O_ELLIPSE :
  138.         objects.ellipses = saved_objects.ellipses->next;
  139.         saved_objects.ellipses->next = NULL;
  140.         erase_ellipses(saved_objects.ellipses);
  141.         break;
  142.         case O_TEXT :
  143.         objects.texts = saved_objects.texts->next;
  144.         saved_objects.texts->next = NULL;
  145.         erase_texts(saved_objects.texts);
  146.         break;
  147.         case O_SPLINE :
  148.             objects.splines = saved_objects.splines->next;
  149.             saved_objects.splines->next = NULL;
  150.         erase_splines(saved_objects.splines);
  151.         break;
  152.         case O_ARC :
  153.         objects.arcs = saved_objects.arcs->next;
  154.         saved_objects.arcs->next = NULL;
  155.         erase_arcs(saved_objects.arcs);
  156.         break;
  157.         case O_COMPOUND :
  158.         objects.compounds = saved_objects.compounds->next;
  159.         saved_objects.compounds->next = NULL;
  160.         erase_compounds(saved_objects.compounds);
  161.         break;
  162.         case O_ALL_OBJECT :
  163.         cut_objects(&objects, &object_tails);
  164.         redisplay_canvas();
  165.         break;
  166.         }
  167.     last_action = F_REMOVE;
  168.     }
  169.  
  170. undo_remove()
  171. {
  172.     switch (last_object) {
  173.         case O_POLYLINE :
  174.         draw_lines(saved_objects.lines);
  175.         insert_line(&objects.lines, saved_objects.lines);
  176.         break;
  177.         case O_ELLIPSE :
  178.         draw_ellipses(saved_objects.ellipses);
  179.         insert_ellipse(&objects.ellipses, saved_objects.ellipses);
  180.         break;
  181.         case O_TEXT :
  182.         draw_texts(saved_objects.texts);
  183.         insert_text(&objects.texts, saved_objects.texts);
  184.         break;
  185.         case O_SPLINE :
  186.         draw_splines(saved_objects.splines);
  187.         insert_spline(&objects.splines, saved_objects.splines);
  188.         break;
  189.         case O_ARC :
  190.         draw_arcs(saved_objects.arcs);
  191.         insert_arc(&objects.arcs, saved_objects.arcs);
  192.         break;
  193.         case O_COMPOUND :
  194.         draw_compounds(saved_objects.compounds);
  195.         insert_compound(&objects.compounds, saved_objects.compounds);
  196.         break;
  197.         case O_ALL_OBJECT :
  198.         append_objects(&objects, &saved_objects, &object_tails);
  199.         redisplay_canvas();
  200.         break;
  201.         }
  202.     last_action = F_CREATE;
  203.     }
  204.  
  205. undo_flip()
  206. {
  207.     switch (last_object) {
  208.         case O_POLYLINE :
  209.         if (pointmarker_shown)
  210.             toggle_linepointmarker(saved_objects.lines);
  211.         draw_line(saved_objects.lines, ERASE);
  212.         flip_line(saved_objects.lines, 
  213.             last_position.x, last_position.y,
  214.             last_axis);
  215.         draw_line(saved_objects.lines, PAINT);
  216.         if (pointmarker_shown)
  217.             toggle_linepointmarker(saved_objects.lines);
  218.         break;
  219.         case O_ELLIPSE :
  220.         if (pointmarker_shown)
  221.             toggle_ellipsepointmarker(saved_objects.ellipses);
  222.         pw_batch_on(canvas_pixwin);
  223.         draw_ellipse(saved_objects.ellipses, background_color);
  224.         flip_ellipse(saved_objects.ellipses, 
  225.             last_position.x, last_position.y,
  226.             last_axis);
  227.         draw_ellipse(saved_objects.ellipses, foreground_color);
  228.         pw_batch_off(canvas_pixwin);
  229.         if (pointmarker_shown)
  230.             toggle_ellipsepointmarker(saved_objects.ellipses);
  231.         break;
  232.         case O_SPLINE :
  233.         if (pointmarker_shown)
  234.             toggle_splinepointmarker(saved_objects.splines);
  235.         pw_batch_on(canvas_pixwin);
  236.         draw_spline(saved_objects.splines, ERASE);
  237.         flip_spline(saved_objects.splines,
  238.             last_position.x, last_position.y,
  239.             last_axis);
  240.         draw_spline(saved_objects.splines, PAINT);
  241.         pw_batch_off(canvas_pixwin);
  242.         if (pointmarker_shown)
  243.             toggle_splinepointmarker(saved_objects.splines);
  244.         break;
  245.         case O_ARC :
  246.         if (pointmarker_shown)
  247.             toggle_arcpointmarker(saved_objects.arcs);
  248.         pw_batch_on(canvas_pixwin);
  249.         draw_arc(saved_objects.arcs, background_color);
  250.         flip_arc(saved_objects.arcs, 
  251.             last_position.x, last_position.y,
  252.             last_axis);
  253.         draw_arc(saved_objects.arcs, foreground_color);
  254.         pw_batch_off(canvas_pixwin);
  255.         if (pointmarker_shown)
  256.             toggle_arcpointmarker(saved_objects.arcs);
  257.         break;
  258.         case O_COMPOUND :
  259.         if (compoundbox_shown) 
  260.             draw_compoundbox(saved_objects.compounds, INV_PAINT);
  261.         erase_compound(saved_objects.compounds);
  262.         flip_compound(saved_objects.compounds, 
  263.             last_position.x, last_position.y,
  264.             last_axis);
  265.         draw_compound(saved_objects.compounds);
  266.         if (compoundbox_shown) 
  267.             draw_compoundbox(saved_objects.compounds, INV_PAINT);
  268.         break;
  269.         }
  270.     }
  271.  
  272. undo_move()
  273. {
  274.     int    dx, dy;
  275.  
  276.     dx = last_position.x - new_position.x;
  277.     dy = last_position.y - new_position.y;
  278.     switch (last_object) {
  279.         case O_POLYLINE :
  280.         if (pointmarker_shown)
  281.             toggle_linepointmarker(saved_objects.lines);
  282.         draw_line(saved_objects.lines, ERASE);
  283.         translate_line(saved_objects.lines, dx, dy);
  284.         draw_line(saved_objects.lines, PAINT);
  285.         if (pointmarker_shown)
  286.             toggle_linepointmarker(saved_objects.lines);
  287.         break;
  288.         case O_ELLIPSE :
  289.         if (pointmarker_shown)
  290.             toggle_ellipsepointmarker(saved_objects.ellipses);
  291.         pw_batch_on(canvas_pixwin);
  292.         draw_ellipse(saved_objects.ellipses, background_color);
  293.         translate_ellipse(saved_objects.ellipses, dx, dy);
  294.         draw_ellipse(saved_objects.ellipses, foreground_color);
  295.         pw_batch_off(canvas_pixwin);
  296.         if (pointmarker_shown)
  297.             toggle_ellipsepointmarker(saved_objects.ellipses);
  298.         break;
  299.         case O_TEXT :
  300.         draw_text(saved_objects.texts, INV_PAINT);
  301.         translate_text(saved_objects.texts, dx, dy);
  302.         draw_text(saved_objects.texts, PAINT);
  303.         break;
  304.         case O_SPLINE :
  305.         if (pointmarker_shown)
  306.             toggle_splinepointmarker(saved_objects.splines);
  307.         pw_batch_on(canvas_pixwin);
  308.         draw_spline(saved_objects.splines, ERASE);
  309.         translate_spline(saved_objects.splines, dx, dy);
  310.         draw_spline(saved_objects.splines, PAINT);
  311.         pw_batch_off(canvas_pixwin);
  312.         if (pointmarker_shown)
  313.             toggle_splinepointmarker(saved_objects.splines);
  314.         break;
  315.         case O_ARC :
  316.         if (pointmarker_shown)
  317.             toggle_arcpointmarker(saved_objects.arcs);
  318.         pw_batch_on(canvas_pixwin);
  319.         draw_arc(saved_objects.arcs, background_color);
  320.         translate_arc(saved_objects.arcs, dx, dy);
  321.         draw_arc(saved_objects.arcs, foreground_color);
  322.         pw_batch_off(canvas_pixwin);
  323.         if (pointmarker_shown)
  324.             toggle_arcpointmarker(saved_objects.arcs);
  325.         break;
  326.         case O_COMPOUND :
  327.         if (compoundbox_shown) 
  328.             draw_compoundbox(saved_objects.compounds, INV_PAINT);
  329.         erase_compound(saved_objects.compounds);
  330.         translate_compound(saved_objects.compounds, dx, dy);
  331.         draw_compound(saved_objects.compounds);
  332.         if (compoundbox_shown) 
  333.             draw_compoundbox(saved_objects.compounds, INV_PAINT);
  334.         break;
  335.         }
  336.     swap_newp_lastp();
  337.     }
  338.  
  339. undo_movepoint()
  340. {
  341.     switch (last_object) {
  342.         case O_POLYLINE :
  343.         relocate_linepoint(saved_objects.lines, 
  344.             last_position.x, last_position.y, 
  345.             saved_objects.lines->points->x,
  346.             saved_objects.lines->points->y, 
  347.             moved_point, left_point);
  348.         break;
  349.         case O_SPLINE :
  350.         relocate_splinepoint(saved_objects.splines, last_position.x,
  351.             last_position.y, moved_point);
  352.         break;
  353.         case O_ARC :
  354.         relocate_arcpoint(saved_objects.arcs, last_position.x, 
  355.             last_position.y, movedpoint_num);
  356.         break;
  357.         case O_ELLIPSE :
  358.         relocate_ellipsepoint(saved_objects.ellipses, last_position.x,
  359.             last_position.y, movedpoint_num);
  360.         break;
  361.         }
  362.     swap_newp_lastp();
  363.     }
  364.  
  365. undo_rotate()
  366. {
  367.     switch (last_object) {
  368.         case O_POLYLINE :
  369.         if (pointmarker_shown)
  370.             toggle_linepointmarker(saved_objects.lines);
  371.         draw_line(saved_objects.lines, ERASE);
  372.         if (last_rotateangle == 90) last_rotateangle = 270;
  373.         else last_rotateangle = 90;
  374.         rotate_line(saved_objects.lines, 
  375.             last_position.x, last_position.y,
  376.             last_rotateangle);
  377.         draw_line(saved_objects.lines, PAINT);
  378.         if (pointmarker_shown)
  379.             toggle_linepointmarker(saved_objects.lines);
  380.         break;
  381.         case O_ELLIPSE :
  382.         if (pointmarker_shown)
  383.             toggle_ellipsepointmarker(saved_objects.ellipses);
  384.         pw_batch_on(canvas_pixwin);
  385.         draw_ellipse(saved_objects.ellipses, background_color);
  386.         if (last_rotateangle == 90) last_rotateangle = 270;
  387.         else last_rotateangle = 90;
  388.         rotate_ellipse(saved_objects.ellipses, 
  389.             last_position.x, last_position.y,
  390.             last_rotateangle);
  391.         draw_ellipse(saved_objects.ellipses, foreground_color);
  392.         pw_batch_off(canvas_pixwin);
  393.         if (pointmarker_shown)
  394.             toggle_ellipsepointmarker(saved_objects.ellipses);
  395.         break;
  396.         case O_SPLINE :
  397.         if (pointmarker_shown)
  398.             toggle_splinepointmarker(saved_objects.splines);
  399.         pw_batch_on(canvas_pixwin);
  400.         draw_spline(saved_objects.splines, ERASE);
  401.         if (last_rotateangle == 90) last_rotateangle = 270;
  402.         else last_rotateangle = 90;
  403.         rotate_spline(saved_objects.splines,
  404.             last_position.x, last_position.y,
  405.             last_rotateangle);
  406.         draw_spline(saved_objects.splines, PAINT);
  407.         pw_batch_off(canvas_pixwin);
  408.         if (pointmarker_shown)
  409.             toggle_splinepointmarker(saved_objects.splines);
  410.         break;
  411.         case O_ARC :
  412.         if (pointmarker_shown)
  413.             toggle_arcpointmarker(saved_objects.arcs);
  414.         pw_batch_on(canvas_pixwin);
  415.         draw_arc(saved_objects.arcs, background_color);
  416.         if (last_rotateangle == 90) last_rotateangle = 270;
  417.         else last_rotateangle = 90;
  418.         rotate_arc(saved_objects.arcs, 
  419.             last_position.x, last_position.y,
  420.             last_rotateangle);
  421.         draw_arc(saved_objects.arcs, foreground_color);
  422.         pw_batch_off(canvas_pixwin);
  423.         if (pointmarker_shown)
  424.             toggle_arcpointmarker(saved_objects.arcs);
  425.         break;
  426.         case O_COMPOUND :
  427.         if (compoundbox_shown)
  428.             draw_compoundbox(saved_objects.compounds, INV_PAINT);
  429.         erase_compound(saved_objects.compounds);
  430.         if (last_rotateangle == 90) last_rotateangle = 270;
  431.         else last_rotateangle = 90;
  432.         rotate_compound(saved_objects.compounds, 
  433.             last_position.x, last_position.y,
  434.             last_rotateangle);
  435.         draw_compound(saved_objects.compounds);
  436.         if (compoundbox_shown)
  437.             draw_compoundbox(saved_objects.compounds, INV_PAINT);
  438.         break;
  439.         }
  440.     }
  441.  
  442. undo_scale()
  443. {
  444.     float    scalex, scaley;
  445.  
  446.     if (compoundbox_shown)
  447.         draw_compoundbox(saved_objects.compounds, INV_PAINT);
  448.     erase_compound(saved_objects.compounds);
  449.     scalex = ((float)(last_position.x-fix_x)) / (new_position.x-fix_x);
  450.     scaley = ((float)(last_position.y-fix_y)) / (new_position.y-fix_y);
  451.     scale_compound(saved_objects.compounds, scalex, scaley, fix_x, fix_y);
  452.     draw_compound(saved_objects.compounds);
  453.     if (compoundbox_shown) draw_compoundbox(saved_objects.compounds, INV_PAINT);
  454.     swap_newp_lastp();
  455.     }
  456.  
  457. swap_newp_lastp()
  458. {
  459.     int    t;  /*  swap new_position and last_position  */
  460.  
  461.     t = new_position.x; 
  462.     new_position.x = last_position.x;
  463.     last_position.x = t;
  464.     t = new_position.y; 
  465.     new_position.y = last_position.y;
  466.     last_position.y = t;
  467.     }
  468.